package com.example.campusforumapi.service.impl;

import com.example.campusforumapi.dao.StatsMapper;
import com.example.campusforumapi.entity.Activity;
import com.example.campusforumapi.entity.Article;
import com.example.campusforumapi.entity.DepartmentStats;
import com.example.campusforumapi.entity.WeeklyStats;
import com.example.campusforumapi.service.StatsService;
import com.example.campusforumapi.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StatsServiceImpl implements StatsService {
    @Autowired
    StatsMapper statsMapper;
    @Override
    public List<WeeklyStats> getWeeklyRegistrations() {
        // 生成最近7天日期列表
        List<LocalDate> dateList = LocalDate.now()
                .minusDays(6)
                .datesUntil(LocalDate.now().plusDays(1))
                .collect(Collectors.toList());
        // 查询数据库
        LocalDateTime start = dateList.get(0).atStartOfDay();
        LocalDateTime end = LocalDateTime.now();
        return statsMapper.getWeeklyRegistrations(start,end);
    }

    @Override
    public List<DepartmentStats> getDepartmentUsers() {
        return statsMapper.getDepartmentUsers();
    }

    @Override
    public List<DepartmentStats> getGenderUsers() {
        return statsMapper.getGenderUsers();
    }

    @Override
    public PageResult getTopArticlePage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Article> topArticleList = statsMapper.getTopArticleList();
        Page<Article> p = (Page<Article>) topArticleList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public PageResult getTopActivityPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Activity> topActivityList = statsMapper.getTopActivityList();
        Page<Activity> p = (Page<Activity>) topActivityList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public List<DepartmentStats> getArticleTags() {
        return statsMapper.getArticleTags();
    }

    @Override
    public List<DepartmentStats> getActivityTags() {
        return statsMapper.getActivityTags();
    }
}
